<script type="text/javascript">
    RED.nodes.registerType('get-daq',{
        category: 'FUXA',
        color: '#a6bbcf',
        defaults: {
            name: {value:""},
            tag: {value:"", required:true},
            from: {value:""},
            to: {value:""}
        },
        inputs:1,
        outputs:1,
        icon: "white-globe.png",
        label: function() {
            return this.name||this.tag||"get daq";
        },
        oneditprepare: function() {
            $.getJSON('/nodered/fuxa/devices', function(data) {
                var datalist = $('#fuxa-tags-daq');
                datalist.empty();
                data.forEach(function(device) {
                    device.tags.forEach(function(tag) {
                        datalist.append('<option value="' + tag.name + '">' + device.name + ' - ' + tag.name + '</option>');
                    });
                });
            });

            // Convert timestamp format for datetime-local inputs
            function formatForInput(timestamp) {
                if (!timestamp) return '';
                // Convert from "YYYY-MM-DD HH:MM:SS" to "YYYY-MM-DDTHH:MM"
                return timestamp.replace(' ', 'T').substring(0, 16);
            }

            function formatForStorage(datetimeLocal) {
                if (!datetimeLocal) return '';
                // Convert from "YYYY-MM-DDTHH:MM" to "YYYY-MM-DD HH:MM:SS"
                return datetimeLocal.replace('T', ' ') + ':00';
            }

            // Set initial values
            $("#node-input-from").val(formatForInput(this.from));
            $("#node-input-to").val(formatForInput(this.to));

            // Handle changes to convert back to storage format
            $("#node-input-from").on('change', function() {
                $("#node-input-from").data('stored-value', formatForStorage($(this).val()));
            });
            $("#node-input-to").on('change', function() {
                $("#node-input-to").data('stored-value', formatForStorage($(this).val()));
            });
        },
        oneditsave: function() {
            // Save the converted values
            this.from = $("#node-input-from").data('stored-value') || $("#node-input-from").val();
            this.to = $("#node-input-to").data('stored-value') || $("#node-input-to").val();
        }
    });
</script>

<script type="text/x-red" data-template-name="get-daq">
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-input-tag"><i class="fa fa-tag"></i> Tag</label>
        <input type="text" id="node-input-tag" list="fuxa-tags-daq" placeholder="Tag Name">
        <datalist id="fuxa-tags-daq"></datalist>
    </div>
    <div class="form-row">
        <label for="node-input-from"><i class="fa fa-clock-o"></i> From (timestamp)</label>
        <input type="datetime-local" id="node-input-from" placeholder="From timestamp (optional)">
    </div>
    <div class="form-row">
        <label for="node-input-to"><i class="fa fa-clock-o"></i> To (timestamp)</label>
        <input type="datetime-local" id="node-input-to" placeholder="To timestamp (optional)">
    </div>
</script>

<script type="text/x-red" data-help-name="get-daq">
    <p>Get DAQ data for a FUXA tag between timestamps.</p>
    <p>The data array is set to <code>msg.payload</code>.</p>
    <h4>Timestamp Configuration</h4>
    <p><strong>Static timestamps</strong> can be set in the node configuration.</p>
    <p><strong>Dynamic timestamps</strong> can override the configured values:</p>
    <ul>
        <li><code>msg.from</code> - Start timestamp (Unix timestamp or ISO string)</li>
        <li><code>msg.to</code> - End timestamp (Unix timestamp or ISO string)</li>
    </ul>
    <p>If no timestamps are provided, defaults to the last hour.</p>
    <h4>Examples</h4>
    <pre>// Get data for last 24 hours
msg.from = Date.now() - 86400000;
msg.to = Date.now();

// Get data for specific date range
msg.from = "2025-01-01 00:00:00";
msg.to = "2025-01-01 23:59:59";</pre>
</script>